package com.pan.vo;


import com.pan.entity.Admin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Arrays;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class AdminVo {
    Long id;
    String name;
    String gender;
    Timestamp birthday;
    Long tel;
    String email;
    Timestamp registerDate;
    Timestamp lastLoginDate;
    Long loginCount;
    Long createId;
    Integer adminStatus;

    public AdminVo(Admin admin){
        Field[] fields =  this.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            String memberName = field.getName();
            String firstUp = memberName.substring(0,1).toUpperCase() + memberName.substring(1);
            Class memberType = field.getType();
            String methodGet = "get"+firstUp;
            String methodSet = "set"+firstUp;
            try {
                Method adminGetMethod = admin.getClass().getMethod(methodGet);
                Method adminVoSetMethod = this.getClass().getMethod(methodSet,memberType);
                Object value = adminGetMethod.invoke(admin);
                adminVoSetMethod.invoke(this,value);
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }

        });
    }
}
